package org.example;

import lombok.extern.slf4j.Slf4j;
import org.example.extend.cmp2.ManualRefreshBeanFactoryPostProcessor;
import org.example.handler.HelloHandler;
import org.example.service.HelloService;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author xiajiqiu
 * @since 2024/6/7 23:50
 */
@Slf4j
@ComponentScan
public class StartFlowApplication {

    public static void main(String[] args) throws Exception {
        long startTime = System.currentTimeMillis();
        String[] configLocations = new String[]{"classpath:applicationContext.xml"};
        // 实例化
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocations, false, null);

        // 第一个扩展点，refresh = false 的时候才是第一个
        context.addBeanFactoryPostProcessor(new ManualRefreshBeanFactoryPostProcessor());
        // 手动 refresh
        context.refresh();
        // 手动调用了这个方法才会发布 ContextStartedEvent 事件
        context.start();

        long cost = System.currentTimeMillis() - startTime;
        log.info("spring context init cost:{}", cost);

        HelloService helloService = context.getBean("helloService", HelloService.class);
        log.info(helloService.hello("Spring"));

        HelloService parentHelloService = context.getBean("parentHelloService", HelloService.class);
        log.info(parentHelloService.hello("ParentSpring"));

        HelloHandler helloHandler = context.getBean("helloHandler", HelloHandler.class);
        log.info(helloHandler.hello("Spring"));

        String applicationName = context.getEnvironment().getProperty("spring.application.name");
        log.info("applicationName:{}", applicationName);

        // 关闭context
        context.close();
        // 手动调用了这个方法才会发布 ContextStoppedEvent 事件
        context.stop();
    }
}
